import 'package:boss/provider/business_setting_provider.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/model/hour_minute.dart';
import 'package:boss/ui/page/takeoutManagement/page/businessSetting/page/timeRange/time_range_page.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/title_switch.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import 'business_setting_header.dart';

class BusinessSettingBrowser extends StatefulWidget {
  @override
  _BusinessSettingBrowserState createState() => _BusinessSettingBrowserState();
}

class _BusinessSettingBrowserState extends State<BusinessSettingBrowser> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(
              horizontal: 30.w,
            ),
            child: Consumer<BusinessSettingProvider>(
              builder: (context, provider, _) {
                return Column(
                  children: <Widget>[
                    BusinessSettingHeader(
                      data: provider.takeoutBusiness,
                      onTimeTap: () async {
                        var res = await Navigator.pushNamed(context, TimeRangePage.pageName, arguments: {
                          'start': provider.takeoutBusiness.startHourMinute,
                          'end': provider.takeoutBusiness.endHourMinute,
                        }) as List<HourMinute>;

                        if (res != null) {
                          provider.setBusinessTime(res[0], res[1]);
                        }
                      },
                    ),
                    BaseTextFormField(
                      initialValue: provider.takeoutBusiness.startMoney == null ? null : AmountUtil.price2String(provider.takeoutBusiness.startMoney),
                      prefixText: '订单起送金额',
                      hintText: '请输入金额',
                      suffixText: '元',
                      inputFormatters: [FilteringTextInputFormatter.allow(RegUtil.numberReg)],
                      keyboardType: TextInputType.numberWithOptions(decimal: true),
                      onChanged: (value) {
                        provider.setStartMoney(value);
                      },
                    ),
                    TitleSwitch(
                      activeColor: Color.fromRGBO(87, 111, 245, 1),
                      title: '到店自提',
                      value: provider.takeoutBusiness.isTakeoutSelfMention,
                      onChanged: (flag) {
                        provider.setTakeoutSelfMention(flag);
                      },
                    ),
                    TitleSwitch(
                      activeColor: Color.fromRGBO(87, 111, 245, 1),
                      title: '营业状态',
                      value: provider.takeoutBusiness.isBusinessStatus,
                      onChanged: (flag) {
                        provider.setBusinessStatus(flag);
                      },
                    ),
                    TitleSwitch(
                      activeColor: Color.fromRGBO(87, 111, 245, 1),
                      title: '是否自动接单',
                      value: provider.takeoutBusiness.isAutoReceive,
                      onChanged: (flag) {
                        provider.setAutoReceive(flag);
                      },
                    ),
                  ],
                );
              },
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保存',
          onTap: () {
            var provider = Provider.of<BusinessSettingProvider>(context, listen: false);
            provider.update().then((e) {}).catchError((e) {});
          },
        ),
      ],
    );
  }
}
